clear all
set more off

////////////////////////////////////////////////
////////////////////////////////////////////////
// Test for other channels: colleges and fire stations
////////////////////////////////////////////////
////////////////////////////////////////////////

global input "...\replication\data"
global output "...\replication\outputs"

use "$input\com_data.dta", clear

****************** Costmetics ******************
** Reomove Corsica
ren com_res com
drop if substr(com,2,1) == "A"
drop if substr(com,2,1) == "B"
destring com, replace

****************** Get dist border ******************
merge m:1 com using "$input\com_arcgis.dta"
drop if _merge != 3
drop _merge
** Drop those within 30km of coast or national border
keep if dist_nat_border < 0 

****************** Get dist border ******************
merge m:1 com using "$input\com_dist_services_fire_school.dta"
drop if _merge != 3
drop _merge

****************** Get neighbor region for all com ******************
tostring com, replace
replace com = "0"+com if strlen(com) == 4
merge m:1 com using loc_i_raw.dta 
drop if _merge == 2
drop _merge
destring com, replace

destring dep_res dep_work, replace
geonear com x_com y_com using loc_j_raw.dta , neighbors(dep x_dep y_dep) nearcount(2)
gen other_dep = nid1 if nid1 != dep_res
replace other_dep = nid2 if nid1 == dep_res

keep com ind dist_dep dep_res same other_dep dep_work fire school dist_schoo dist_fire

****************** Get main vars******************
** Labor markets at home and on the other side
egen lab_mkts = sum(ind), by(dep_work)
preserve
collapse (mean) lab_mkts, by(dep_work)
ren dep_work dep
save lab_mkts.dta, replace
restore

* Get size of home lab market
ren dep_res dep
merge m:1 dep using lab_mkts.dta
drop _merge
ren dep dep_res
ren lab_mkts home_lab_mkt

* Get size of lab market behinf the border
ren other_dep dep
merge m:1 dep using lab_mkts.dta
drop _merge
ren dep other_dep
ren lab_mkts other_lab_mkt


****************** Get main vars******************

replace dist_dep = -dist_dep if same == 0

drop if same == 0 & other_lab_mkt > home_lab_mkt
drop if same == -1 & other_lab_mkt < home_lab_mkt

gen Treat = 0 if other_lab_mkt < home_lab_mkt
replace Treat = 1 if other_lab_mkt > home_lab_mkt


****************** Results in Figure ******************
rdbwselect fire dist_dep, c(0) 
local opt_h=e(h_mserd)
rdplot fire dist_dep if abs(dist_dep) < `opt_h', nbins(7 7) ci(90) p(2) ///
graph_options(legend(off) scheme(s1color)  ///
title("Smaller market ___________________ Larger market") ///
ytitle("Presence of fire station") ///
xtitle("Distance to the border (km)")  legend(off))
graph export "$output\rdplot_fire.pdf", replace


rdbwselect school dist_dep, c(0) 
local opt_h=e(h_mserd)
rdplot school dist_dep if abs(dist_dep) < `opt_h',  nbins(7 7) ci(90) p(2) ///
graph_options(legend(off) scheme(s1color) ///
title("Smaller market ___________________ Larger market") ///
ytitle("Presence of secondary school") ///
xtitle("Distance to the border (km)")  legend(off))
graph export "$output\rdplot_school.pdf", replace

****************** Results in Figure ******************
rdbwselect dist_fire dist_dep, c(0) 
local opt_h=e(h_mserd)
rdplot fire dist_dep if abs(dist_dep) < `opt_h', nbins(7 7) ci(90) p(2) ///
graph_options(legend(off) scheme(s1color)  ///
title("Smaller market ___________________ Larger market") ///
ytitle("Distance to nearest fire station (m)") ///
xtitle("Distance to the border (km)")  legend(off))
graph export "$output\rdplot_dist_fire.pdf", replace


rdbwselect dist_schoo dist_dep, c(0) 
local opt_h=e(h_mserd)
rdplot school dist_dep if abs(dist_dep) < `opt_h',  nbins(7 7) ci(90) p(2) ///
graph_options(legend(off) scheme(s1color) ///
title("Smaller market ___________________ Larger market") ///
ytitle("Distance to nearest secondary school (m)") ///
xtitle("Distance to the border (km)")  legend(off))
graph export "$output\rdplot_dist_school.pdf", replace

